package zjut.raymond.collectorplus.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import zjut.raymond.collectorplus.R;
import zjut.raymond.collectorplus.pojo.Area;

public class SelectListAdapter extends RecyclerView.Adapter<SelectListAdapter.MyViewHolder> {
    //List<String> list = new ArrayList<>();
    List<Area> list = new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    //设置回调接口
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setList(List<Area> list) {
        this.list = list;
    }

    public List<Area> getList() {
        return list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView = layoutInflater.inflate(R.layout.item_choice_list, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        if (!TextUtils.isEmpty(list.get(position).getName())) {
            holder.tvInfo.setText(list.get(position).getName());
        }
        holder.itemView.setOnClickListener(v -> onItemClickListener.onItemClick(v, position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvInfo;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvInfo = itemView.findViewById(R.id.tv_item_select);
        }
    }
}
